* **************************************************************************** *
* Sierra Leone - Social Signaling and Childhood Immunization                   *
* Positive and Neagtive Externalities of Immunization                          *
* **************************************************************************** *
/*
** Purpose:    Create Tables on Externalities:

              * 1) Social and Private Benefits of Vaccinations (positive externalities)
              * 2) Knowledge of Externalities  (negative externalities)

*/
********************************************************************************
********************************************************************************


	use  "${Replicate_SocialSignals_dtaInter}/Endline_Survey_Data.dta", clear

	* Merge in the ANC treatment status:
	merge m:m clinic  using  "${Replicate_SocialSignals_dtaInter}/MasterData_PilotClinics.dta", gen(ANCtreat)


	* Label the treatment variable:
	lab var treat3   "Signal at 4"
	lab var treat4   "Signal at 5"
	lab var treat2   "Uninformative Bracelet"

	* -----------------------------------------------------------------------------
	* Demean control variables
	tab     arm_anc, gen(anc)

	foreach control in anc2 anc3 anc4  {
		sum  `control' if intervention_arm==1, detail
		gen  `control'_dm     = `control' - `r(mean)'
	}

	tab     education_cat, gen(educ)

	local  ControlVars = " mother_age_w01 educ2 educ3 farm  birth2 birth3 birth4 birth5 "

	foreach control of local  ControlVars  {
		summ `control' if intervention_arm==1, detail
		gen  `control'_dm     = `control' - `r(mean)'
	}

	local  ControlVars = " mother_age_w01_dm educ2_dm educ3_dm farm_dm birth2_dm birth3_dm birth4_dm birth5_dm "
	local  ANC_dm      = " anc2_dm anc3_dm anc4_dm "

	summ  `ControlVars' `ANC_dm'



	save "${Replicate_SocialSignals_dtaFinal}/Endline_Externalities_constructed.dta", replace

	********************************************************************************
	* Externaltities - Positive and Negative
	********************************************************************************

	use   "${Replicate_SocialSignals_dtaFinal}/Endline_Externalities_constructed.dta", clear


	local  ControlVars = " mother_age_w01_dm educ2_dm educ3_dm farm_dm birth2_dm birth3_dm birth4_dm birth5_dm "
	local  ANC_dm      = " anc2_dm anc3_dm anc4_dm "


	eststo clear
	
	foreach variable in vacc_helpful1  vacc_commhelpful2 vacc_others_harm2 vacc_child_harm2 {
		areg   `variable'  treat3 treat4 treat2   `ControlVars'    `ANC_dm', absorb(strata) vce(bootstrap , reps(${RepsNum}) seed(${seed}) cluster(clinic))
		eststo
		test   treat3 = treat2
		estadd scalar treat2_treat3 = r(p)
		test   treat3 = treat4
		estadd scalar treat3_treat4 = r(p)
		test   treat2 = treat4
		estadd scalar treat2_treat4 = r(p)
		test   treat2 treat3 treat4
		estadd scalar bracelets     = r(p)
		estadd local Obs            = "`e(N)'"
		estadd scalar C_mean        = _b[_cons]
		estadd local Controls       = "Yes"
		eststo model_`variable'
	}

	esttab  ///
	model_vacc_helpful1     model_vacc_commhelpful2    ///
	model_vacc_others_harm2  model_vacc_child_harm2    ///
	using "${Replicate_SocialSignals_ATables}/Table_VaccinationsHelpful_raw.tex",            ///
	prehead("\begin{tabular}{l*{5}{c}} \toprule \\"                       ///
	"\multicolumn{1}{l}{\textbf{\textbf{Dependent Variable:}}} & \multicolumn{1}{c}{\textbf{Vaccinations are helpful}}   & \multicolumn{1}{c}{\textbf{My child’s vaccination can be helpful}} & \multicolumn{1}{c}{\textbf{\textbf{My child can be harmful to others}}}  & \multicolumn{1}{c}{\textbf{\textbf{Other children can be harmful}}} \\"     ///
	"                                                          & \multicolumn{1}{c}{\textbf{for my own child's health}}  & \multicolumn{1}{c}{\textbf{for other children in the community}}   & \multicolumn{1}{c}{\textbf{\textbf{if she/he is not immunized}}}         & \multicolumn{1}{c}{\textbf{\textbf{to my child if not immunized}}}   \\ ")  ///
	scalars("C_mean           Control Group mean"                       ///
	"Obs              Observations"                             ///
	"treat2_treat3    \(S_{4}\) \(>\) 0: p(UI = S4)"            ///
	"treat2_treat4    \(S_{5}\) \(>\) 0: p(UI = S5)"            ///
	"treat3_treat4    p(S4 = S5)"                               ///
	"bracelets Joint  F-Test"                                   ///
	"Controls         Controls")                                ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	keep(_cons treat3 treat4 treat2)                                      ///
	varlabels(_cons "Control Group mean")                                 ///
	sfmt(3) ${StarsOpt}  nomtitles                                        ///
	addnotes("") label b(3) se(3)  nobaselevels noconstant noobs          ///
	nolines posthead("\midrule") postfoot("\bottomrule \end{tabular}") tex  ///
	replace

	filefilter "${Replicate_SocialSignals_ATables}/Table_VaccinationsHelpful_raw.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_Externalities_Combined_Controls.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_VaccinationsHelpful_raw.tex"



	* ----------------------------------------------------------------------------
	* -> outsheet p-values: without controls

	esttab  ///
	model_vacc_helpful1     model_vacc_commhelpful2    ///
	model_vacc_others_harm2  model_vacc_child_harm2    ///
	using "${Replicate_SocialSignals_ATables}/Table_VaccinationsHelpful_raw.tex",            ///
	prehead("\begin{tabular}{l*{5}{c}} \toprule \\"                       ///
	"\multicolumn{1}{l}{\textbf{\textbf{Dependent Variable:}}} & \multicolumn{1}{c}{\textbf{Vaccinations are helpful}}   & \multicolumn{1}{c}{\textbf{My child’s vaccination can be helpful}} & \multicolumn{1}{c}{\textbf{\textbf{My child can be harmful to others}}}  & \multicolumn{1}{c}{\textbf{\textbf{Other children can be harmful}}} \\"     ///
	"                                                          & \multicolumn{1}{c}{\textbf{for my own child's health}}  & \multicolumn{1}{c}{\textbf{for other children in the community}}   & \multicolumn{1}{c}{\textbf{\textbf{if she/he is not immunized}}}         & \multicolumn{1}{c}{\textbf{\textbf{to my child if not immunized}}}   \\ ")  ///
	scalars("C_mean           Control Group mean"                       ///
	"Obs              Observations"                             ///
	"treat2_treat3    \(S_{4}\) \(>\) 0: p(UI = S4)"            ///
	"treat2_treat4    \(S_{5}\) \(>\) 0: p(UI = S5)"            ///
	"treat3_treat4    p(S4 = S5)"                               ///
	"bracelets Joint  F-Test"                                   ///
	"Controls         Controls")                                ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))              ///
	keep(_cons treat3 treat4 treat2)                                    ///
	varlabels(_cons "Control Group mean")                               ///
	sfmt(3) ${StarsOpt}  nomtitles                                      ///
	addnotes("") label b(3) p(3)  nobaselevels noconstant noobs         ///
	nolines posthead("\midrule") postfoot("\bottomrule \end{tabular}") tex  ///
	replace

	filefilter "${Replicate_SocialSignals_ATables}/Table_VaccinationsHelpful_raw.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_Externalities_Combined_Controls_pvals.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_VaccinationsHelpful_raw.tex"

	erase	     "${Replicate_SocialSignals_ATables}/Table_Externalities_Combined_Controls_pvals.tex"



* **************************************************************************** *
* End of the Dofile !!!
* **************************************************************************** *
